var layer = layui.layer //弹层
    ,table = layui.table //表格
    form = layui.form,$ = layui.jquery;
layer.ready(function () {
    //获取仓库信息
    $.ajax({
        url:"transfer/getWarehouse",
        success:function(data){
            if(data.status == 200){
                for(let i in data.data){
                    $("#warehouseCode").append("<option value=\"" + data.data[i].code + "\">" + data.data[i].name + "</option>");
                }
                form.render('select');
            }
        }
    });
})
form.render('select');

table.render({
    elem: '#product'
    ,url: 'otherOutStock/add/getStockInfoByPage'
    ,cols: [[
        {checkbox: true, sort: true,title:'选择'}
        ,{field:'contractNo', width: 120, title:'销售合同号', sort: true}
        ,{field:'purchaseNo', width: 120, title:'采购单号', sort: true}
        ,{field:'warehouseName', width: 120, title:'仓库名称', sort: true}
        ,{field:'productCode', width: 120, title:'产品编码', sort: true}
        ,{field:'specificationCode', width: 120, title:'规格编码', sort: true}
        ,{field:'specificationExplanation', width: 120, title:'规格说明', sort: true}
        ,{field:'stockQuantity', width: 120, title:'库存数量', sort: true}
        ,{field:'canApplyQuantity', width: 120, title:'剩余数量', sort: true}
        ,{field:'stockCost', width: 120, title: '成本单价', sort: true}
        ,{field:'shareCostPrice', width: 160, title: '分摊费用单价', sort: true}
        ,{field:'productUnit',width: 100, title:'单位', sort: true}
        ,{field:'clientProductNo', width: 120, title:'客户货号', sort: true}
        ,{field:'productCname', width: 120, title:'中文名称', sort: true}
        ,{field:'productEname', width: 120, title:'英文名称', sort: true}
        ,{field:'productCdesc', width: 120, title: '中文描述', sort: true}
        ,{field:'productEdesc', width: 120, title: '英文描述', sort: true}
        ,{field:'lotNo', width: 100, title: '批次号', sort: true}
        ,{field:'trayNumber', width: 120, title: '托盘入数', sort: true}
        ,{field:'totalTray', width: 120, title: '总托盘', sort: true}
    ]]
    ,id: 'producttableid'
    ,page: true
    , parseData: function(res){
        return {
            "code": res.status,
            "count": res.total,
            "data": res.data
        };
    }
    ,height: 600
});

form.on('submit(submitform)', function(data){
    table.reload('producttableid', {
        page: {
            limit:10,
            curr: 1 //重新从第 1 页开始
        }
        ,where: data.field
    }, 'data');
    return false;
});

$("#choose").on("click",function () {
    let productData = table.checkStatus("producttableid").data;
    if(productData.length == 0){
        layer.msg("请选择数据")
    }else{
        let oldData =  parent.table.cache["storageproductstableid"];
        $.each(productData, function (index, value) {
            //剔除重复数据
            let flag = true;
            if(oldData.length > 0){
                for(var i = 0; i < oldData.length; i++){
                    if(oldData[i].stockId == productData[index].stockId){
                        flag = false;
                        break;
                    }
                }
            }
            if(flag){
                //不存在重复数据,添加数据
                oldData.push(productData[index]);
            }
        });
        parent.table.reload('storageproductstableid',{data : oldData, limit: oldData.length});
        //执行接口
        setTimeout(function () {
            //保存成功则关闭此页面
            let index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        },)
    }
})
$("#shutdown").on("click",function () {
    let index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
})